package com.qm.console.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.ImmutableMap;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.pubdef.dto.jw.SpiderArticleQueryDTO;
import com.qm.console.pubdef.jw.dto.SpiderArticleDTO;
import com.qm.console.service.jw.JWContentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.controller.rest
 * Created on 2021/8/23.
 */
@RestController
@RequestMapping("/jw/content")
public class JwContentController {

    @Autowired
    private JWContentService jwContentService;

    @GetMapping("articles")
    public BaseRsp listSpiderArticles(SpiderArticleQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "list", jwContentService.list(queryDTO),
                "count", jwContentService.count(queryDTO)
        ));
    }

    @PutMapping("article")
    public BaseRsp updateArticle(@RequestBody SpiderArticleDTO update) {

        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("id", jwContentService.insertOrUpdate(update)));
    }

    @DeleteMapping("article")
    public BaseRsp deleteArticle(@RequestBody JSONArray ids) {
        jwContentService.delete(ids.stream().map(i-> Integer.valueOf(String.valueOf(i))).collect(Collectors.toList()));
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("robot/random")
    public BaseRsp randomRobot() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("robot", jwContentService.randomRobot()));
    }

    @PostMapping("article/recommend")
    public BaseRsp addToRecommend(@RequestBody JSONArray ids) {
        jwContentService.addToRecommend(ids);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

}
